<template>
  <el-card style="width: 500px;">
    <el-form label-width="80px" size="small">
      <el-upload
          class="avatar-uploader"
          :action="'http://' + serverIp +':8081/file/upload'"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
      >
        <img v-if="form.avatarurl" :src="form.avatarurl" class="avatar">
        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
      </el-upload>
      <el-form label-width="80px" size="small">
        <el-form-item label="账号">
            <el-input v-model="form.administratorAccount" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码">
            <el-input v-model="form.administratorPassword" autocomplete="off"></el-input>
        </el-form-item>
    </el-form>
      <el-form-item>
        <el-button type="primary" @click="save">确 定</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script>
    import {
        serverIp
    } from "../../public/config";
    export default {
        name: "Person",
        data() {
            return {
                serverIp: serverIp,
                form: {},
                admin: localStorage.getItem("admin") ? JSON.parse(localStorage.getItem("admin")) : {}
            }
        },
        created() {
            this.getAdmin().then(res => {
                    console.log(res),
                        this.form = res
                })
                // this.request.get("/admin/administratorAccount/" + this.admin.administratorAccount).then(res => {
                //     if (res.data === '200') {
                //         console.log(res)
                //         this.form = res.data
                //     }
                // })
        },
        methods: {
            async getAdmin() {
                return (await this.request.get("/admin/Account/" + this.admin.administratorAccount)).data
            },
            save() {
                this.request.post("/admin", this.form).then(res => {
                    if (res.code === '200') {
                        this.$message.success("保存成功")

                        // 触发父级更新User的方法
                        this.$emit("refreshAdmin")

                        // 更新浏览器存储的用户信息
                        this.getAdmin().then(res => {
                            res.token = JSON.parse(localStorage.getItem("admin")).token
                            localStorage.setItem("admin", JSON.stringify(res))
                        })

                    } else {
                        this.$message.error("保存失败")
                    }
                })
            },
            // save() {
            //     this.request.post("/admin", this.form).then(res => {
            //         if (res.data) {
            //             this.$message.success("保存成功")
            //                 // this.dialogFormVisible = false

            //         } else {
            //             this.$message.error("保存失败")
            //         }
            //     })
            // },
            handleAvatarSuccess(res) {
                this.form.avatarurl = res
            }
        }
    }
</script>

<style>
    .avatar-uploader {
        text-align: center;
        padding-bottom: 10px;
    }
    
    .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }
    
    .avatar-uploader .el-upload:hover {
        border-color: #409EFF;
    }
    
    .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 138px;
        height: 138px;
        line-height: 138px;
        text-align: center;
    }
    
    .avatar {
        width: 138px;
        height: 138px;
        display: block;
    }
</style>